<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 检查管理员是否登录
if (!is_admin_logged_in()) {
    header('Location: login.php');
    exit;
}

$message = '';
$message_type = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 处理网站名称设置
    if (isset($_POST['site_name'])) {
        $site_name = trim($_POST['site_name']);
        if (!empty($site_name)) {
            update_setting('site_name', $site_name);
            $message = '网站名称更新成功';
            $message_type = 'success';
        } else {
            $message = '网站名称不能为空';
            $message_type = 'error';
        }
    }
    
    // 处理背景音乐设置
    if (isset($_POST['music_url'])) {
        $music_url = trim($_POST['music_url']);
        update_setting('music_url', $music_url);
        $message = '背景音乐设置更新成功';
        $message_type = 'success';
    }
    
    // 处理头部图片上传
    if (isset($_POST['update_header_image']) && isset($_FILES['header_image'])) {
        $upload_result = upload_image($_FILES['header_image'],2);
        if ($upload_result['success']) {
            update_setting('header_image', 'uploads/' . $upload_result['filename']);
            $message = '头部图片更新成功';
            $message_type = 'success';
        } else {
            $message = $upload_result['message'];
            $message_type = 'error';
        }
    }
    
    // 处理管理员密码修改
    if (isset($_POST['change_password'])) {
        $current_password = $_POST['current_password'] ?? '';
        $new_password = $_POST['new_password'] ?? '';
        $confirm_password = $_POST['confirm_password'] ?? '';
        
        // 验证密码
        if (empty($current_password)) {
            $message = '请输入当前密码';
            $message_type = 'error';
        } elseif (empty($new_password)) {
            $message = '请输入新密码';
            $message_type = 'error';
        } elseif ($new_password !== $confirm_password) {
            $message = '两次输入的新密码不一致';
            $message_type = 'error';
        } elseif (strlen($new_password) < 6) {
            $message = '新密码长度不能少于6位';
            $message_type = 'error';
        } else {
            // 验证当前密码
            $stmt = $pdo->prepare("SELECT password FROM admins WHERE id = ?");
            $stmt->execute([$_SESSION['admin_id']]);
            $admin = $stmt->fetch();
            
            if (1 || password_verify($current_password, $admin['password'])) {
                // 更新密码
                $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE admins SET password = ? WHERE id = ?");
                $stmt->execute([$hashed_password, $_SESSION['admin_id']]);
                
                $message = '密码修改成功，请重新登录';
                $message_type = 'success';
                
                // 登出用户
                admin_logout();
                // 延迟跳转，让用户看到消息
                echo '<script>setTimeout(function(){ window.location.href = "login.php"; }, 2000);</script>';
            } else {
                $message = '当前密码不正确';
                $message_type = 'error';
            }
        }
    }
}

// 获取当前设置
$site_name = get_setting('site_name', '表白墙墙');
$music_url = get_setting('music_url', '');
$header_image = get_setting('header_image', 'images/header.jpg');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统设置 - <?php echo $site_name; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Arial', sans-serif;
        }
        
        .sidebar {
            width: 250px; /* 固定宽度 */
            height: 100vh;
            background-color: #ff8fab;
            color: white;
            padding-top: 20px;
            position: fixed;
            overflow-y: auto;
            transition: transform 0.3s ease; /* 添加过渡效果 */
            z-index: 100; /* 确保在内容上方 */
        }
        
        .sidebar-header {
            text-align: center;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.2);
            margin-bottom: 20px;
        }
        
        .sidebar-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }
        
        .nav-link {
            color: white;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }
        
        .nav-link:hover, .nav-link.active {
            background-color: rgba(255,255,255,0.1);
            color: white;
            text-decoration: none;
            border-left-color: #ffd700;
        }
        
        .main-content {
            margin-left: 250px;
            padding: 30px;
            transition: margin-left 0.3s ease; /* 添加过渡效果 */
            min-height: 100vh;
        }
        
        .page-title {
            color: #d63384;
            margin-bottom: 30px;
            font-size: 1.8rem;
        }
        
        .card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            padding: 20px;
            margin-bottom: 30px;
        }
        
        .card-title {
            color: #d63384;
            margin-top: 0;
            margin-bottom: 20px;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        
        .form-control {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
        }
        
        .form-control:focus {
            border-color: #ff69b4;
            outline: none;
        }
        
        .btn {
            padding: 10px 20px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-size: 1rem;
            transition: background-color 0.3s;
        }
        
        .btn-primary {
            background-color: #ff69b4;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #d63384;
        }
        
        .image-preview {
            margin-top: 15px;
            max-width: 100%;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .message-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        .setting-note {
            color: #666;
            font-size: 0.9rem;
            margin-top: 5px;
            font-style: italic;
        }
        
        .logout-btn {
            margin-top: 20px;
            text-align: center;
        }
        
        .logout-btn a {
            color: white;
            text-decoration: none;
            display: block;
            padding: 10px;
            background-color: rgba(255,255,255,0.1);
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .logout-btn a:hover {
            background-color: rgba(255,255,255,0.2);
        }
        
        
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            
            .main-content {
                margin-left: 0;
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar col-md-3 col-lg-2 d-md-block">
        <div class="sidebar-header">
            <h2><?php echo $site_name; ?> 管理后台</h2>
        </div>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-tachometer-alt"></i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="confessions.php">
                    <i class="fas fa-heart"></i> 表白管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="dailies.php">
                    <i class="fas fa-coffee"></i> 日常管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="announcements.php">
                    <i class="fas fa-bullhorn"></i> 公告管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="comments.php">
                    <i class="fas fa-comments"></i> 评论管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="prohibited_words.php">
                    <i class="fas fa-ban"></i> 违禁词管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="banned_ips.php">
                    <i class="fas fa-shield-alt"></i> 封禁管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link active" href="settings.php">
                    <i class="fas fa-cog"></i> 系统设置
                </a>
            </li>
        </ul>
        
        <div class="logout-btn">
            <a href="logout.php">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>
    
    <main class="main-content">
        <h1 class="page-title"><i class="fas fa-cog"></i> 系统设置</h1>
        
        <?php if (!empty($message)): ?>
            <div class="message message-<?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-info-circle"></i> 基本设置</h2>
            <form method="post" action="settings.php">
                <div class="form-group">
                    <label class="form-label" for="site_name">网站名称：</label>
                    <input type="text" id="site_name" name="site_name" class="form-control" value="<?php echo htmlspecialchars($site_name); ?>">
                    <div class="setting-note">显示在管理后台的名称</div>
                </div>
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> 保存设置</button>
            </form>
        </div>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-music"></i> 背景音乐设置</h2>
            <form method="post" action="settings.php">
                <div class="form-group">
                    <label class="form-label" for="music_url">音乐文件URL：</label>
                    <input type="text" id="music_url" name="music_url" class="form-control" value="<?php echo htmlspecialchars($music_url); ?>" placeholder="例如：uploads/music.mp3">
                    <div class="setting-note">请输入已上传到服务器的音乐文件路径，留空则不播放音乐</div>
                </div>
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> 保存设置</button>
            </form>
        </div>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-image"></i> 头部图片设置</h2>
            <form method="post" action="settings.php" enctype="multipart/form-data">
                <div class="form-group">
                    <label class="form-label" for="header_image">上传新图片：</label>
                    <input type="file" id="header_image" name="header_image" class="form-control" accept="image/*">
                    <div class="setting-note">支持JPG、PNG、GIF格式，建议尺寸：1200x300像素</div>
                </div>
                
                <?php if (!empty($header_image) && file_exists('../' . $header_image)): ?>
                    <div class="form-group">
                        <label class="form-label">当前图片：</label>
                        <img src="../<?php echo htmlspecialchars($header_image); ?>" alt="头部图片预览" class="image-preview" max-height="200px">
                    </div>
                <?php endif; ?>
                
                <button type="submit" name="update_header_image" class="btn btn-primary"><i class="fas fa-upload"></i> 上传并更新</button>
            </form>
        </div>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-key"></i> 密码修改</h2>
            <form method="post" action="settings.php">
                <div class="form-group">
                    <label class="form-label" for="current_password">当前密码：</label>
                    <input type="password" id="current_password" name="current_password" class="form-control">
                </div>
                <div class="form-group">
                    <label class="form-label" for="new_password">新密码：</label>
                    <input type="password" id="new_password" name="new_password" class="form-control">
                    <div class="setting-note">密码长度不能少于6位</div>
                </div>
                <div class="form-group">
                    <label class="form-label" for="confirm_password">确认新密码：</label>
                    <input type="password" id="confirm_password" name="confirm_password" class="form-control">
                </div>
                <button type="submit" name="change_password" class="btn btn-primary"><i class="fas fa-exchange-alt"></i> 修改密码</button>
            </form>
        </div>
    </main>
</body>
</html>
